package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.PreUnitMapper;
import com.xidian.zhiancloud.dao.PreUnitTypeMapper;
import com.xidian.zhiancloud.dao.PuInMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.equipFacility.PreUnitQO;
import com.xidian.zhiancloud.qo.equipFacility.PuInQO;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyMaterialsInventoryManagementVO;
import com.xidian.zhiancloud.vo.equipFacility.PreUnitVO;
import com.xidian.zhiancloud.vo.equipFacility.PuInVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PuInService extends ServiceImpl<PuInMapper, PuIn> implements IService<PuIn> {

    @Autowired
    private PuInMapper puInMapper;
    @Autowired
    private PreUnitMapper preUnitMapper;
    @Autowired
    private PreUnitTypeMapper preUnitTypeMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean add(PuIn puIn) {
        int result = puInMapper.insert(puIn);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        Integer numIn = puIn.getRuNum();
        Long puId = puIn.getPuId();
        PreUnit preUnit = preUnitMapper.selectById(puId);
        Integer numNow = preUnit.getNum();
        preUnit.setNum(numIn + numNow);
        int i = preUnitMapper.updateById(preUnit);
        return i == 1;
    }

    public IPage<PuInVO> getPuIn(PuInQO puInQO) {
        Page<PuInVO> page = new Page<>(puInQO.getCurrent(), puInQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("ru_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<PuInVO> ans=puInMapper.selectByQO(page,puInQO);
        for (PuInVO record : ans.getRecords()) {
            PreUnit preUnit=preUnitMapper.selectById(record.getPuId());
            //record.setPuCode(preUnit==null?"该备件不存在":preUnit.getPuCode());
            record.setPuName(preUnit==null?"该备件不存在":preUnit.getName());
            PreUnitType preUnitType=preUnitTypeMapper.selectById(preUnit.getPreUnitTypeId());
            record.setPerUnitTypeName(preUnitType==null?"该备件类型不存在":preUnitType.getName());
            record.setModel(preUnit==null?"该备件不存在":preUnit.getModel());
            Organization org = organizationMapper.selectById(preUnit.getMngCtrDeptId());
            record.setOrganizationName(org==null?"该部门不存在":org.getName());
            SysUser userid =sysUserMapper.selectById(record.getRuer());
            record.setCreator(userid==null?"该用户不存在":userid.getName());
        }
        return ans;
    }


}
